from functools import reduce

# lambda匿名函数

map1 = map(lambda data_x: data_x ** 2, range(9))  # x: 0,1,2,3 ...
'''map(func, *iterables) --> map object'''
print(list(map1))
'''等价于'''
items = range(9)
list1 = []
for i in items:
    list1.append(i ** 2)
print(list1)  # [0, 1, 4, 9, 16, 25, 36, 49, 64]


'''filter(function or None, iterable) --> filter object'''
print(list(filter(lambda x: (x - 1) % 2 == 0, range(20))))  # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

'''reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) == ((((1+2)+3)+4)+5)'''
print(reduce(lambda x, y: x + y, range(20)))  # 190 == 0 + 1 + 2 + ... + 19


def multiply(x):
    return x * x


def add(x):
    return x + x


funcs = [multiply, add]
for i in range(5):
    print(list(map(lambda func_x: func_x(i), funcs)))  # x: [multiply, add]
'''
[0, 0]
[1, 2]
[4, 4]
[9, 6]
[16, 8]
'''

